<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace app\admin\controller;

use app\portal\model\MyRoleModel;
use app\admin\model\RoleModel;
use app\admin\controller\RbacController;
use think\db\Query;

/**
 * Description of RbacMineController
 *
 * @author PC
 */
class RbacMineController extends RbacController
{
    /**
     * 角色管理列表
     * @adminMenu(
     *     'name'   => '角色管理',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '角色管理',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $content = hook_one('admin_rbac_index_view');

        if (!empty($content)) {
            return $content;
        }

        $filter = $this->request->param();

        $model = new MyRoleModel();
        $query = $model->alias('cr');
        $query->leftJoin('cmf_role_user cru', 'cru.role_id = cr.id');
        $data = $query->field('cr.*, count(cru.user_id) as role_count')
            ->where(function (Query $query) use ($filter) {
                if (!empty($filter['name'])) {
                    $query->where('cr.name', 'like', "%{$filter['name']}%");
                }
                if (!empty($filter['type'])) {
                    $query->where('cr.type', 'like', "%{$filter['type']}%");
                }                
            })
            ->group('cr.id')
            ->select();

        $this->assign('name', isset($filter['name']) ? $filter['name'] : '');
        $this->assign('type', isset($filter['type']) ? $filter['type'] : '');
        $this->assign("roles", $data);
        return $this->fetch();
    }


}
